﻿#pragma once
#include <stdlib.h>
#include <time.h>
#include "SelectionSort.h"
#include "QuickSort.h"
#include "MergeSort.h"
#include "HeapSort.h"

namespace doanpplthdtctdl {


	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::Collections::Generic;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 

	private: System::Windows::Forms::RadioButton^  radioButton1;
	private: System::Windows::Forms::RadioButton^  radioButton2;
	private: System::Windows::Forms::RadioButton^  radioButton5;
	private: System::Windows::Forms::RadioButton^  radioButton6;
	private: System::Windows::Forms::Button^  button1;

	private: System::Windows::Forms::Button^  button2;
	private: System::Windows::Forms::Button^  button4;


	private: System::Windows::Forms::TextBox^  textBox8;
	private: System::Windows::Forms::TextBox^  textBox7;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  button3;


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		List <int> ^a;
	private: System::Windows::Forms::Timer^  timer1;
	private: System::ComponentModel::IContainer^  components;


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->radioButton1 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton2 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton5 = (gcnew System::Windows::Forms::RadioButton());
			this->radioButton6 = (gcnew System::Windows::Forms::RadioButton());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->button2 = (gcnew System::Windows::Forms::Button());
			this->button4 = (gcnew System::Windows::Forms::Button());
			this->textBox8 = (gcnew System::Windows::Forms::TextBox());
			this->textBox7 = (gcnew System::Windows::Forms::TextBox());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->button3 = (gcnew System::Windows::Forms::Button());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->SuspendLayout();
			// 
			// radioButton1
			// 
			this->radioButton1->AutoSize = true;
			this->radioButton1->Location = System::Drawing::Point(135, 145);
			this->radioButton1->Name = L"radioButton1";
			this->radioButton1->Size = System::Drawing::Size(77, 17);
			this->radioButton1->TabIndex = 1;
			this->radioButton1->TabStop = true;
			this->radioButton1->Text = L"Merge Sort";
			this->radioButton1->UseVisualStyleBackColor = true;
			// 
			// radioButton2
			// 
			this->radioButton2->AutoSize = true;
			this->radioButton2->Location = System::Drawing::Point(226, 145);
			this->radioButton2->Name = L"radioButton2";
			this->radioButton2->Size = System::Drawing::Size(73, 17);
			this->radioButton2->TabIndex = 1;
			this->radioButton2->TabStop = true;
			this->radioButton2->Text = L"Heap Sort";
			this->radioButton2->UseVisualStyleBackColor = true;
			// 
			// radioButton5
			// 
			this->radioButton5->AutoSize = true;
			this->radioButton5->Location = System::Drawing::Point(135, 116);
			this->radioButton5->Name = L"radioButton5";
			this->radioButton5->Size = System::Drawing::Size(91, 17);
			this->radioButton5->TabIndex = 1;
			this->radioButton5->TabStop = true;
			this->radioButton5->Text = L"Selection Sort";
			this->radioButton5->UseVisualStyleBackColor = true;
			// 
			// radioButton6
			// 
			this->radioButton6->AutoSize = true;
			this->radioButton6->Location = System::Drawing::Point(226, 116);
			this->radioButton6->Name = L"radioButton6";
			this->radioButton6->Size = System::Drawing::Size(75, 17);
			this->radioButton6->TabIndex = 1;
			this->radioButton6->TabStop = true;
			this->radioButton6->Text = L"Quick Sort";
			this->radioButton6->UseVisualStyleBackColor = true;
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(122, 72);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(186, 37);
			this->button1->TabIndex = 2;
			this->button1->Text = L"GENERATE";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// button2
			// 
			this->button2->Location = System::Drawing::Point(122, 206);
			this->button2->Name = L"button2";
			this->button2->Size = System::Drawing::Size(186, 32);
			this->button2->TabIndex = 2;
			this->button2->Text = L"HELP";
			this->button2->UseVisualStyleBackColor = true;
			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
			// 
			// button4
			// 
			this->button4->Location = System::Drawing::Point(122, 168);
			this->button4->Name = L"button4";
			this->button4->Size = System::Drawing::Size(186, 32);
			this->button4->TabIndex = 2;
			this->button4->Text = L"START";
			this->button4->UseVisualStyleBackColor = true;
			this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
			// 
			// textBox8
			// 
			this->textBox8->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox8->Location = System::Drawing::Point(359, 21);
			this->textBox8->Multiline = true;
			this->textBox8->Name = L"textBox8";
			this->textBox8->Size = System::Drawing::Size(42, 37);
			this->textBox8->TabIndex = 6;
			this->textBox8->Text = L"0";
			this->textBox8->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox8->UseWaitCursor = true;
			// 
			// textBox7
			// 
			this->textBox7->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox7->Location = System::Drawing::Point(311, 21);
			this->textBox7->Multiline = true;
			this->textBox7->Name = L"textBox7";
			this->textBox7->Size = System::Drawing::Size(42, 37);
			this->textBox7->TabIndex = 7;
			this->textBox7->Text = L"0";
			this->textBox7->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox7->UseWaitCursor = true;
			// 
			// textBox6
			// 
			this->textBox6->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox6->Location = System::Drawing::Point(263, 21);
			this->textBox6->Multiline = true;
			this->textBox6->Name = L"textBox6";
			this->textBox6->Size = System::Drawing::Size(42, 37);
			this->textBox6->TabIndex = 8;
			this->textBox6->Text = L"0";
			this->textBox6->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox6->UseWaitCursor = true;
			// 
			// textBox5
			// 
			this->textBox5->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox5->Location = System::Drawing::Point(215, 21);
			this->textBox5->Multiline = true;
			this->textBox5->Name = L"textBox5";
			this->textBox5->Size = System::Drawing::Size(42, 37);
			this->textBox5->TabIndex = 9;
			this->textBox5->Text = L"0";
			this->textBox5->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox5->UseWaitCursor = true;
			// 
			// textBox4
			// 
			this->textBox4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox4->Location = System::Drawing::Point(167, 21);
			this->textBox4->Multiline = true;
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(42, 37);
			this->textBox4->TabIndex = 10;
			this->textBox4->Text = L"0";
			this->textBox4->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox4->UseWaitCursor = true;
			// 
			// textBox3
			// 
			this->textBox3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox3->Location = System::Drawing::Point(119, 21);
			this->textBox3->Multiline = true;
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(42, 37);
			this->textBox3->TabIndex = 11;
			this->textBox3->Text = L"0";
			this->textBox3->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox3->UseWaitCursor = true;
			// 
			// textBox2
			// 
			this->textBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox2->Location = System::Drawing::Point(71, 21);
			this->textBox2->Multiline = true;
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(42, 37);
			this->textBox2->TabIndex = 12;
			this->textBox2->Text = L"0";
			this->textBox2->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox2->UseWaitCursor = true;
			// 
			// textBox1
			// 
			this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox1->Location = System::Drawing::Point(23, 21);
			this->textBox1->Multiline = true;
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(42, 37);
			this->textBox1->TabIndex = 13;
			this->textBox1->Text = L"0";
			this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Center;
			this->textBox1->UseWaitCursor = true;
			// 
			// button3
			// 
			this->button3->Location = System::Drawing::Point(122, 244);
			this->button3->Name = L"button3";
			this->button3->Size = System::Drawing::Size(186, 32);
			this->button3->TabIndex = 2;
			this->button3->Text = L"EXIT";
			this->button3->UseVisualStyleBackColor = true;
			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(427, 296);
			this->Controls->Add(this->textBox8);
			this->Controls->Add(this->textBox7);
			this->Controls->Add(this->textBox6);
			this->Controls->Add(this->textBox5);
			this->Controls->Add(this->textBox4);
			this->Controls->Add(this->textBox3);
			this->Controls->Add(this->textBox2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->button3);
			this->Controls->Add(this->button2);
			this->Controls->Add(this->button4);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->radioButton6);
			this->Controls->Add(this->radioButton5);
			this->Controls->Add(this->radioButton2);
			this->Controls->Add(this->radioButton1);
			this->Name = L"Form1";
			this->Text = L"Sorting Algorithms Demo";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(MessageBox::Show("Are you really want to Exit?","Warning!", MessageBoxButtons::YesNo) == System::Windows::Forms::DialogResult::Yes)
					 Close();
				 else
					 return;
				 
			 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			int num[8] = { 0 };
			srand((int) time(0));
			for (int i = 0; i < 8; i++) 
			{
				num[i] = (rand() % 99) + 1;
				for(int j=i-1; j>=-1; j--)
					if(num[i] == num[j])
						num[i] = (rand() % 99) + 1;
				
			}

			a = gcnew List<int>();

			for(int i = 0; i<8 ; i++)
			{
				a->Add(num[i]);
			}

			this->textBox1->Text = Convert::ToString(num[0]);
			this->textBox2->Text = Convert::ToString(num[1]);
			this->textBox3->Text = Convert::ToString(num[2]);
			this->textBox4->Text = Convert::ToString(num[3]);
			this->textBox5->Text = Convert::ToString(num[4]);
			this->textBox6->Text = Convert::ToString(num[5]);
			this->textBox7->Text = Convert::ToString(num[6]);
			this->textBox8->Text = Convert::ToString(num[7]);
		
		 }
private: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
			 if(textBox1->Text == "0" && textBox2->Text == "0")
			 {
				 if(MessageBox::Show("You must click the GENERATE button before you click START.","Warning!",MessageBoxButtons::OK,MessageBoxIcon::Warning) == System::Windows::Forms::DialogResult::OK)
					 return;
			 }

			 else 
			 {
				 if(radioButton5->Checked)
				 {
					 SelectionSort s_sort;
					 for(int i = 0; i<8 ; i++)
					 {
						 s_sort.Input(a[i],i);
					 }
				
					
					 s_sort.SeletionSort();
					 for(int i = 0; i<8 ; i++)
					 {
						 int temp[8];
						 s_sort.Output(temp[i],i);
						 for(int i = 0; i<8 ; i++)
						 {
							 a[i] = temp[i];
						 }
					 }
					 this->textBox1->Text = Convert::ToString(a[0]);
					 this->textBox2->Text = Convert::ToString(a[1]);
					 this->textBox3->Text = Convert::ToString(a[2]);
					 this->textBox4->Text = Convert::ToString(a[3]);
					 this->textBox5->Text = Convert::ToString(a[4]);
					 this->textBox6->Text = Convert::ToString(a[5]);
					 this->textBox7->Text = Convert::ToString(a[6]);
					 this->textBox8->Text = Convert::ToString(a[7]);
				 }
				 else if(radioButton6->Checked)
				 {
					 QuickSort q_sort;
					 for(int i = 0; i<8 ; i++)
					 {
						 q_sort.Input(a[i],i);
					 }
					 q_sort.Quick_Sort();
					 for(int i = 0; i<8 ; i++)
					 {
						 int temp[8];
						 q_sort.Output(temp[i],i);
						 for(int i = 0; i<8 ; i++)
						 {
							 a[i] = temp[i];
						 }
					 }
					 this->textBox1->Text = Convert::ToString(a[0]);
					 this->textBox2->Text = Convert::ToString(a[1]);
					 this->textBox3->Text = Convert::ToString(a[2]);
					 this->textBox4->Text = Convert::ToString(a[3]);
					 this->textBox5->Text = Convert::ToString(a[4]);
					 this->textBox6->Text = Convert::ToString(a[5]);
					 this->textBox7->Text = Convert::ToString(a[6]);
					 this->textBox8->Text = Convert::ToString(a[7]);
				 }
				 else if(radioButton1->Checked)
				 {
					 MergeSort m_sort;
					 for(int i = 0; i<8 ; i++)
					 {
						 m_sort.Input(a[i],i);
					 }
					 m_sort.Merge_Sort();
					 for(int i = 0; i<8 ; i++)
					 {
						 int temp[8];
						 m_sort.Output(temp[i],i);
						 for(int i = 0; i<8 ; i++)
						 {
							 a[i] = temp[i];
						 }
					 }
					 this->textBox1->Text = Convert::ToString(a[0]);
					 this->textBox2->Text = Convert::ToString(a[1]);
					 this->textBox3->Text = Convert::ToString(a[2]);
					 this->textBox4->Text = Convert::ToString(a[3]);
					 this->textBox5->Text = Convert::ToString(a[4]);
					 this->textBox6->Text = Convert::ToString(a[5]);
					 this->textBox7->Text = Convert::ToString(a[6]);
					 this->textBox8->Text = Convert::ToString(a[7]);
				 }
				 else if(radioButton2->Checked)
				 {
					 HeapSort h_sort;
					 for(int i = 0; i<8 ; i++)
					 {
						 h_sort.Input(a[i],i);
					 }
					 h_sort.Heap_Sort();
					 for(int i = 0; i<8 ; i++)
					 {
						 int temp[8];
						 h_sort.Output(temp[i],i);
						 for(int i = 0; i<8 ; i++)
						 {
							 a[i] = temp[i];
						 }
					 }
					 this->textBox1->Text = Convert::ToString(a[0]);
					 this->textBox2->Text = Convert::ToString(a[1]);
					 this->textBox3->Text = Convert::ToString(a[2]);
					 this->textBox4->Text = Convert::ToString(a[3]);
					 this->textBox5->Text = Convert::ToString(a[4]);
					 this->textBox6->Text = Convert::ToString(a[5]);
					 this->textBox7->Text = Convert::ToString(a[6]);
					 this->textBox8->Text = Convert::ToString(a[7]);
				 }
			}
		 }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
			 if(MessageBox::Show("Click GENERATE button to randomize a number array.\nUse the radio button to choose the sorting algorithm you want to operate.\nClick START button to sort.\nClick HELP button to see the help dialog.\nClick EXIT button to exit the application.","HELP",MessageBoxButtons::OK) == System::Windows::Forms::DialogResult::OK)
				 return;
		 }
};
}

